<template>
    <div>
        <h1>医生添加</h1>
        <table class="table">
            <tr>
                <td>姓名</td>
                <td>
                    <input type="text" v-model="data.yname">
                </td>
            </tr>

            <tr>
                <td>头像</td>
                <td>
                    <input type="file" @change="addimg">
                    <img :src="'https://localhost:7215/'+data.imgUrl" style="width: 60px;height: 60px;">
                </td>
            </tr>

            <tr>
                <td>科室</td>
                <td>
                    <select v-model="data.kid">
                        <option value="0">请选择科室</option>
                        <option v-for="d in xl" :value="d.kid">{{ d.kname }}</option>
                    </select>
                </td>
            </tr>

            <tr>
                <td></td>
                <td>
                    <input type="button" value="添加" class="btn btn-success" @click="add">
                </td>
            </tr>
        </table>
    </div>
</template>

<script setup lang="ts">
import {ref,onMounted} from 'vue'
import axios from 'axios'
import { useRouter } from 'vue-router';
const router=useRouter()

const data=ref({
  "yname": "",
  "imgUrl": "",
  "kid": 0
})
//上传
const addimg=(a:any)=>{
    var f=a.target.files[0]
    var fs =new FormData()
    fs.append("file",f)
    debugger
    if(f.size>1024*1024*2){
        alert('图片大小不能超过2MB');
        return;
    }
    axios.post("/api/Ys/GetImg",fs).then(res=>{
        data.value.imgUrl=res.data
    })
}
//添加
const add=()=>{
    if(data.value.yname==""){
        alert('医生姓名不能为空');
        return;
    }
    axios.post("/api/Ys/AddYs",data.value).then(res=>{
        if(res.data>0){
            alert('添加成功');
            router.push("/ysshow")
        }
        else{
            alert('添加失败');
            return
        }
    })
}

//文档就绪
onMounted(()=>{
    bang()
})
//绑定
const xl=ref({})
const bang=()=>{
    axios.get("/api/Ks/GetKsXl").then(res=>{
        xl.value=res.data
    })
}
</script>

<style scoped>

</style>